class Solution
{
public:
    int minNumberOfHours(int initialEnergy, int initialExperience, vector<int> &energy, vector<int> &experience)
    {
        int n = energy.size();
        int curEnergy = initialEnergy;
        int curExperience = initialExperience;
        int energyDiff = 0;
        int experienceDiff = 0;
        for (int i = 0; i < n; ++i)
        {
            if (curEnergy <= energy[i])
            {
                energyDiff += energy[i] + 1 - curEnergy;
                curEnergy = 1;
            }
            else
            {
                curEnergy -= energy[i];
            }

            if (curExperience <= experience[i])
            {
                experienceDiff += experience[i] + 1 - curExperience;
                curExperience = 2 * experience[i] + 1;
            }
            else
            {
                curExperience += experience[i];
            }
        }
        return experienceDiff + energyDiff;
    }
};